package com.ebraga.trackerclub.android.util;

import android.os.Build;
import android.util.Log;

/**
 * Debug logger. Only logs debug messages when in debug mode. 
 * 
 * @author JVanhoof
 */
public class DLog {

	private static final String DEFAULT_TAG = "AndroidTracker";
	private static final String DEFAULT_MESSAGE = "";
	private static final String TAG_PREFIX = "TRACKER_";
    private static final int MAX_TAG_LENGTH = 30;
	
	@SuppressWarnings("rawtypes")
	public static String logTag(Class cls) {
        //return cls.toString();
        return logTag(cls.getSimpleName());
    }
	
	public static String logTag(String str) {
        if (str.length() > MAX_TAG_LENGTH - TAG_PREFIX.length()) {
            return TAG_PREFIX + str.substring(0, MAX_TAG_LENGTH - TAG_PREFIX.length() - 1);
        }
        return TAG_PREFIX + str;
    }

	private static String notNull(String s) {
		if (null == s) {
			s = "NULL";
		}
		return s;
	}

	public static void d(String message) {
		DLog.d(DEFAULT_TAG, message);
	}

	public static void d(String tag, String message) {
		// Only log when in debug mode !!! <----------
		if (inDebugMode()) {
			message = notNull(message);
			Log.d(tag, message);
		}
	}
	
	/**
	 * Are we running in the emulator? Is the DEBUG flag set? 
	 * 
	 * @return boolean
	 */
	public static boolean inDebugMode() {
		return ("google_sdk".equals(Build.PRODUCT)) || ("sdk".equals(Build.PRODUCT));
	}

	public static void i(String message) {
		DLog.i(DEFAULT_TAG, message);
	}

	public static void i(String tag, String message) {
		message = notNull(message);
		Log.i(tag, message);
	}

	public static void v(String message) {
		DLog.v(DEFAULT_TAG, message);
	}

	public static void v(String tag, String message) {
		message = notNull(message);
		Log.v(tag, message);
	}

	public static void e(Throwable t) {
		DLog.e(DEFAULT_TAG, DEFAULT_MESSAGE, t);
	}

	public static void e(String message, Throwable t) {
		DLog.e(DEFAULT_TAG, message, t);
	}

	public static void e(String tag, String message, Throwable t) {
		message = notNull(message);
		Log.e(tag, message, t);
	}

	public static void w(Throwable t) {
		DLog.w(DEFAULT_TAG, DEFAULT_MESSAGE, t);
	}

	public static void w(String message, Throwable t) {
		DLog.w(DEFAULT_TAG, message, t);
	}

	public static void w(String tag, String message, Throwable t) {
		message = notNull(message);
		Log.w(tag, message, t);
	}

	public static void wtf(Throwable t) {
		DLog.wtf(DEFAULT_TAG, DEFAULT_MESSAGE, t);
	}

	public static void wtf(String message, Throwable t) {
		DLog.wtf(DEFAULT_TAG, message, t);
	}

	public static void wtf(String tag, String message, Throwable t) {
		message = notNull(message);
		Log.wtf(tag, message, t);
	}

	public static void wtf(String tag, String message) {
		message = notNull(message);
		Log.wtf(tag, message);
	}

}
